package com.kayang.feishu.controller;


import com.kayang.common.HttpMsg;

import com.kayang.common.util.MapUtil;
import com.kayang.feishu.config.FeiShuConfig;
import com.kayang.feishu.service.AccessTokenService;
import com.kayang.feishu.pojo.DeptModel;
import com.kayang.feishu.pojo.FeiShuContext;
import com.kayang.feishu.service.DeptService;
import com.kayang.nadapt.util.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value = "/dept")
public class DeptController extends BaseController {

    @Autowired
    AccessTokenService tokenService;

    @Autowired
    FeiShuConfig feiShuConfig;

    @Autowired
    DeptService deptService;

    @PostMapping("/create")
    public ResponseEntity creatDept(@Valid @RequestBody DeptModel deptModel){
        String url = feiShuConfig.getPath() + "/open-apis/contact/v3/departments?client_token={client_token}&department_id_type={department_id_type}&user_id_type={user_id_type}";
        Map param = MapUtil.toMap(deptModel);
        url = url.replace("{client_token}", Convert.toString(param.get("client_token")))
                .replace("{department_id_type}", Convert.toString(param.get("department_id_type")))
                .replace("{user_id_type}", Convert.toString(param.get("user_id_type")));
        param.remove("client_token");
        param.remove("department_id_type");
        param.remove("user_id_type");
        FeiShuContext context = new FeiShuContext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(param);
        Map header = new HashMap();
        header.put("Authorization", "Bearer " + tokenService.getAccessTokenStr(feiShuConfig.getAppKey()));
        header.put("Content-Type", "application/json; charset=utf-8");
        Map ret = deptService.creatDept(param, context, header);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("创建成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @PostMapping("/update")
    public ResponseEntity updateDept(@Valid @RequestBody DeptModel deptModel){
        String url = feiShuConfig.getPath() + "/open-apis/contact/v3/departments/{department_id}?department_id_type={department_id_type}&user_id_type={user_id_type}";
        Map param = MapUtil.toMap(deptModel);
        url = url.replace("{department_id}", Convert.toString(param.get("department_id")))
                .replace("{department_id_type}", Convert.toString(param.get("department_id_type")))
                .replace("{user_id_type}", Convert.toString(param.get("user_id_type")));
        param.remove("department_id");
        param.remove("open_department_id");
        param.remove("user_id_type");
        FeiShuContext context = new FeiShuContext();
        context.setUrl(url);
        context.setMethod("PUT");
        context.setBody(param);
        Map header = new HashMap();
        header.put("Authorization", "Bearer " + tokenService.getAccessTokenStr(feiShuConfig.getAppKey()));
        header.put("Content-Type", "application/json; charset=utf-8");
        Map ret = deptService.updateDept(param, context, header);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("更新成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }


    @PostMapping("/delete")
    public ResponseEntity deleteDept( @RequestBody Map param){
        String url = feiShuConfig.getPath() + "/open-apis/contact/v3/departments/{department_id}?department_id_type={department_id_type}";
        url = url.replace("{department_id}", Convert.toString(param.get("department_id")))
                .replace("{department_id_type}", Convert.toString(param.get("department_id_type")));
        param.remove("department_id");
        param.remove("department_id_type");
        FeiShuContext context = new FeiShuContext();
        context.setUrl(url);
        context.setMethod("DELETE");
        context.setBody(param);
        Map header = new HashMap();
        header.put("Authorization", "Bearer " + tokenService.getAccessTokenStr(feiShuConfig.getAppKey()));
        header.put("Content-Type", "application/json; charset=utf-8");
        Map ret = deptService.deleteDept(param, context, header);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("删除成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }


    @PostMapping("/get")
    public ResponseEntity getDept( @RequestBody Map param){
        String url = feiShuConfig.getPath() + "/open-apis/contact/v3/departments/{department_id}?department_id_type={department_id_type}&user_id_type={user_id_type}";
        url = url.replace("{department_id}", Convert.toString(param.get("department_id")))
                .replace("{user_id_type}", Convert.toString(param.get("user_id_type")))
                .replace("{department_id_type}", Convert.toString(param.get("department_id_type")));
        FeiShuContext context = new FeiShuContext();
        context.setUrl(url);
        context.setMethod("GET");
        context.setBody(param);
        Map header = new HashMap();
        header.put("Authorization", "Bearer " + tokenService.getAccessTokenStr(feiShuConfig.getAppKey()));
        header.put("Content-Type", "application/json; charset=utf-8");
        Map ret = deptService.getDept(context, header);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("查看成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }




}